#include <openssl/asn1.h>
#include <openssl/bn.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/param_build.h>
#include <openssl/types.h>
#include <openssl/x509.h>
#include "print.h"

enum {
    FAIL = -1,
    OK = 0,
};

int EncodeAsn1(uint8_t *rData, int rSz, uint8_t *sData, int sSz, uint8_t **result)
{
    BIGNUM *bgR = BN_bin2bn(rData, rSz, NULL);
    BIGNUM *bgS = BN_bin2bn(sData, sSz, NULL);
    ASN1_INTEGER *asn1R = BN_to_ASN1_INTEGER(bgR, NULL);
    ASN1_INTEGER *asn1S = BN_to_ASN1_INTEGER(bgS, NULL);
    ASN1_TYPE *typeR = ASN1_TYPE_new();
    ASN1_TYPE *typeS = ASN1_TYPE_new();
    ASN1_TYPE_set(typeR, V_ASN1_INTEGER, asn1R);
    ASN1_TYPE_set(typeS, V_ASN1_INTEGER, asn1S);
    ASN1_SEQUENCE_ANY *seq = sk_ASN1_TYPE_new_null();
    sk_ASN1_TYPE_push(seq, typeR);
    sk_ASN1_TYPE_push(seq, typeS);
    int len = i2d_ASN1_SET_ANY(seq, result);
    BN_free(bgR);
    BN_free(bgS);
    sk_ASN1_TYPE_pop_free(seq, ASN1_TYPE_free);
    return len;
}

int main()
{
    uint8_t rData[32] = {
        0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44,
    };
    uint8_t sData[32] = {
        0xaa, 0xbb, 0xcc, 0xdd, 0xaa, 0xbb, 0xcc, 0xdd,
    };
    uint8_t *result = NULL;
    int len = EncodeAsn1(rData, sizeof(rData), sData, sizeof(sData), &result);
    PrintBuffer("result", result, len);
    return OK;
}